#!/usr/bin/env python
#
# compile vc++ projects for netbarvideo 
#

import os
import sys
import shutil


def help(): 
	print "usage: perl ./compile.pl [build|rebuild|clean] [debug|release] [project name]"
	print "    [project name]: vmascli"
	print "    [project name]: vmasinstall"
	print "    [project name]: vmasunstall"
	print "    [project name]: vmasuptool"
	print "    [project name]: vmasupcheck"
	print "    [project name]: realplayocx"
	print "    [project name]: playbackocx"
	print "    [project name]: regocx"
	print "    [project name]: vmasextract"
	print "    [project name]: chtpcli"
	print "    [project name]: all"


def svnup(svnpath):
	cmd = "svn up %s" % svnpath
	print "cmd=", cmd
	status = os.system(cmd)
	print "svn up status=", status
	return status == 0


def compile(opt1, opt2, projectname):
	cmd = 'devenv ../bin/CATIM.sln /%s "%s|Win32" /project %s' % (opt1, opt2, projectname)
	print "cmd=", cmd
	status = os.system(cmd)
	print "exec status=", status
	return status == 0


def copy_file(src, dst):
	if False == os.path.exists( src ): 
		print 'no found: %s' % src
		return False
	if False == shutil.copyfile( src, dst ):
		print '%s->%s copy fail' % (src, dst)
		return False
	if False == os.path.exists( dst ): 
		print 'no found: %s' % dst
		return False
	return True


def wzzip(dst, src_list):
	if True == os.path.exists( dst ): 
		os.remove( dst )

	cmd = 'wzzip -a %s %s -r -P' % (dst, src_list)
	print "cmd=", cmd
	status = os.system(cmd)
	print "exec status=", status
	if False == os.path.exists( dst ): 
		print 'no found: %s' % dst
		return False
	return True


def compile_regocx():
	realplayocx_src = "../bin/%s/RealPlayCtrl.ocx" % sys.argv[2]
	realplayocx_dst = "./vmas/regocx/res/install/RealPlayCtrl.ocx"
	if False == copy_file( realplayocx_src, realplayocx_dst ):
		return False

	playbackocx_src = "../bin/%s/PlayBackCtrl.ocx" % sys.argv[2]
	playbackocx_dst = "./vmas/regocx/res/install/PlayBackCtrl.ocx"
	if False == copy_file( playbackocx_src, playbackocx_dst ):
		return False

	dst = "install.zip"
	src_list = "install"
	os.chdir("./vmas/regocx/res")
	if False == wzzip(dst, src_list):
		return False
	os.chdir("../../../")

	if False == svnup( "vmas/regocx" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "RegOcx")


def compile_vmasextract():
	src = "../bin/%s/Setup.exe" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/Setup.exe"
	if False == copy_file( src, dst ):
		return False

	src = "../bin/%s/VMAS.exe" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/files/VMAS.exe"
	if False == copy_file( src, dst ):
		return False

	src = "../bin/%s/VMASUnInst.exe" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/files/VMASUnInst.exe"
	if False == copy_file( src, dst ):
		return False

	src = "../bin/%s/VMASUpCheck.exe" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/files/VMASUpCheck.exe"
	if False == copy_file( src, dst ):
		return False

	src = "../bin/%s/VMASUpTool.exe" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/files/VMASUpTool.exe"
	if False == copy_file( src, dst ):
		return False

	src = "../bin/%s/CARes.dll" % sys.argv[2]
	dst = "./vmas/install/VMASExtract/res/install/files/CARes.dll"
	if False == copy_file( src, dst ):
		return False

	dst = "install.zip"
	src_list = "install"
	os.chdir("./vmas/install/VMASExtract/res")
	if False == wzzip(dst, src_list):
		return False
	os.chdir("../../../../")

	if False == svnup( "vmas/install/VMASExtract" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMASExtract")


def compile_vmascli():
	if False == svnup( "vmas/vmascli" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMAS")


def compile_vmasinstall():
	if False == svnup( "vmas/install/VMASInstall"):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMASInstall")


def compile_vmasunstall():
	if False == svnup( "vmas/uninstall"):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMASUnInst")


def compile_vmasuptool():
	if False == svnup( "vmas/update/VMASTool"):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMASUpTool")


def compile_vmasupcheck():
	if False == svnup( "vmas/update/VMASUpCheck" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "VMASUpCheck")


def compile_realplayocx():
	if False == svnup( "vmas/vedioctrl/realplay" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "RealPlayCtrl")


def compile_playbackocx():
	if False == svnup( "vmas/vedioctrl/palyback" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "PlayBackCtrl")


def compile_chtpcli():
	if False == svnup( "vmas/chtpcli" ):
		return False
	return compile( sys.argv[1], sys.argv[2], "CHTP")


def compile_all():
	if False == compile_vmascli():
		return
	if False == compile_vmasinstall():
		return
	if False == compile_vmasunstall():
		return 
	if False == compile_vmasuptool():
		return 
	if False == compile_vmasupcheck():
		return 
	if False == compile_realplayocx():
		return 
	if False == compile_playbackocx():
		return 
	if False == compile_regocx():
		return 
	if False == compile_vmasextract():
		return
	compile_chtpcli()


def main():
	if len(sys.argv) < 4:
		help()
		return
	elif sys.argv[1] != "build" and sys.argv[1] != "rebuild" and sys.argv[1] != "clean":
		help()
		return
	elif sys.argv[2] != "debug" and sys.argv[2] != "release":
		help()
		return

	if sys.argv[3] == "vmascli":
		compile_vmascli()
	elif sys.argv[3] == "vmasinstall":
		compile_vmasinstall()
	elif sys.argv[3] == "vmasunstall":
		compile_vmasunstall()
	elif sys.argv[3] == "vmasuptool":
		compile_vmasuptool()
	elif sys.argv[3] == "vmasupcheck":
		compile_vmasupcheck()
	elif sys.argv[3] == "realplayocx":
		compile_realplayocx()
	elif sys.argv[3] == "playbackocx":
		compile_playbackocx()
	elif sys.argv[3] == "regocx":
		compile_regocx()
	elif sys.argv[3] == "vmasextract":
		compile_vmasextract()
	elif sys.argv[3] == "chtpcli":
		compile_chtpcli()
	elif sys.argv[3] == "all":
		compile_all()
	else:
		help()


if __name__ == "__main__":
	main()


